package lgq.xuan.httprequest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import lgq.xuan.httprequest.phone.manager.CalledPermissionManager;
import lgq.xuan.httprequest.phone.manager.MessageSendPermissionManager;
import lgq.xuan.httprequest.phone.manager.itf.CheckPermission;
import lgq.xuan.httprequest.util.sqlite.PhoneCURD;
import lgq.xuan.httprequest.util.sqlite.PhoneHelper;

public class DetailsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        Intent intent = getIntent();
        int id = intent.getIntExtra(PhoneListActivity.DETAILS_PHONE, -1);
        boolean is_update = intent.getBooleanExtra(PhoneListActivity.IS_UPDATE, false);

        SQLiteOpenHelper phoneHelper = new PhoneHelper(this);

        try (SQLiteDatabase db = phoneHelper.getReadableDatabase()) {
            Cursor cursor = db.query("phone", new String[]{"name", "called", "phone"}, "_id = ?", new String[]{Integer.toString(id)}, null, null, null);

            if (cursor.moveToFirst()) {
                String name = cursor.getString(0);
                EditText n = findViewById(R.id.detailName);
                n.setText(name);

                String called = cursor.getString(1);
                EditText c = findViewById(R.id.detailCalled);
                c.setText(called);

                String phone = cursor.getString(2);
                EditText p = findViewById(R.id.detailPhone);
                p.setText(phone);

                if (!is_update) {
                    disableInput();
                } else {
                    Button eas = findViewById(R.id.edit_and_save);
                    eas.setText(getString(R.string.save));
                }

            }
        } catch (SQLException e) {
            e.getMessage();
        }
    }

    private void disableInput() {
        EditText n = findViewById(R.id.detailName);
        EditText c = findViewById(R.id.detailCalled);
        EditText p = findViewById(R.id.detailPhone);

        n.setFocusable(false);
        n.setFocusableInTouchMode(false);
        c.setFocusable(false);
        c.setFocusableInTouchMode(false);
        p.setFocusable(false);
        p.setFocusableInTouchMode(false);

    }

    private void enableInput() {
        EditText n = findViewById(R.id.detailName);
        EditText c = findViewById(R.id.detailCalled);
        EditText p = findViewById(R.id.detailPhone);

        n.setFocusable(true);
        n.setFocusableInTouchMode(true);
        n.requestFocus();

        c.setFocusable(true);
        c.setFocusableInTouchMode(true);
        c.requestFocus();

        p.setFocusable(true);
        p.setFocusableInTouchMode(true);
        p.requestFocus();
    }

    public void toEdit(View view) {
        Button button = (Button) view;
        String buttonText = button.getText().toString();

        EditText n = findViewById(R.id.detailName);
        EditText c = findViewById(R.id.detailCalled);
        EditText p = findViewById(R.id.detailPhone);
        if (buttonText.equals(getString(R.string.edit))) {
            enableInput();
            button.setText(R.string.save);
        } else if (buttonText.equals(getString(R.string.save))) {
            Intent intent = getIntent();
            int id = intent.getIntExtra(PhoneListActivity.DETAILS_PHONE, -1);

            String name = n.getText().toString();
            String called = c.getText().toString();
            String phone = p.getText().toString();

            PhoneCURD.update(this, id, name, called, phone);
            Toast.makeText(this, "修改已保存", Toast.LENGTH_SHORT).show();
            disableInput();
            button.setText(R.string.edit);
        }

    }

    public void cancel(View view) {
        onBackPressed();
    }

    /**
     * 拨号
     */
    public void call(View view) {
        EditText phoneView = findViewById(R.id.detailPhone);
        String phone = phoneView.getText().toString();

        CheckPermission callPermission = new CalledPermissionManager(this);

        if (!callPermission.checkAllNeedPermission()) {
            callPermission.requestAllNeedPermission();
            return;
        }

        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        startActivity(intent);
    }

    /**
     * 发短信
     */
    public void sendMsg(View view) {
        EditText phoneView = findViewById(R.id.detailPhone);
        String phone = phoneView.getText().toString();

        CheckPermission messagePermission = new MessageSendPermissionManager(this);

        if (!messagePermission.checkAllNeedPermission()) {
            messagePermission.requestAllNeedPermission();
            return;
        }

        if (PhoneNumberUtils.isGlobalPhoneNumber(phone)) {
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            Uri data = Uri.parse("smsto:" + phone);
            intent.setData(data);
            intent.putExtra("sms_body", "");
            startActivity(intent);
        } else {
            Toast.makeText(this, "电话号码不正确", Toast.LENGTH_SHORT).show();
        }
    }
}
